package com.example.config;

import com.example.interceptor.MyInterceptor01;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@Configuration
public class CorsConfig implements WebMvcConfigurer{
    /*二、添加拦截器配置*/
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor01())
                .addPathPatterns("/**").
                excludePathPatterns("/hello");
    }

    /**
     * 三、spring boot注册了拦截器 导致静态资源映射失效，手动进行映射
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("classpath:/yl213/");
    }
    /**
     * 路径映射
     * addViewController 配置浏览器访问的路径
     * setViewName  通过thymeleaf的视图名称 前缀(classpath:/templates/)+视图名称+后缀(.html)
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/user/toAdd").setViewName("user/userAdd");
        registry.addViewController("/index").setViewName("index");
    }

    /**
     * 一、配置cors跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/cors/**")
        .allowedHeaders("*")
        .allowedMethods ("*")
        .maxAge(1800)
        .allowedOrigins("http://localhost:8081");
    }
}
